﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Login
{
//模拟多文档应用程序
//1、单文档SDI
//2、多文档MDI（MDI窗口不能ShowDialog）
//1）设置容器窗口IsMdiContainer
//2）需要指定子窗口的MdiParent
//3）显示子窗口

//窗口实例只能产生一个（单例模式）（对象间的数据交换）

    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void miChangePwd_Click(object sender, EventArgs e)
        {
            FormChangePwd frmPwd = new FormChangePwd();
            frmPwd.TopMost = true;
            frmPwd.ShowDialog();
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            //主窗口关闭   Application.Exit
            //Close();
            Application.Exit();
        }

        private void miLogOff_Click(object sender, EventArgs e)
        {            
            Application.Restart();
        }

        private void miCalc_Click(object sender, EventArgs e)
        {
            Process.Start("calc.exe");
        }

        void mi_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
            CreateInstance(tsmi.Tag.ToString(), tsmi);
        }

        void CreateInstance(string ClassName, ToolStripMenuItem sender)
        {
            Type type = Type.GetType(ClassName);
            Form frm = (Form)Activator.CreateInstance(type, sender);
            frm.MdiParent = this;
            if (sender is ToolStripMenuItem)
            {
                (sender as ToolStripMenuItem).Enabled = false;
            }
            frm.Show();
            ToolStripMenuItem tsmi = new ToolStripMenuItem();
            miWins.DropDownItems.Add(tsmi);
            tsmi.Text = frm.Text;
            tsmi.Click += Tsmi_Click;
            frm.Tag = tsmi;
            tsmi.Tag = frm;
        }

        void FormShow(Form frm, object tsmi)
        {
            frm.MdiParent = this;
            frm.Show();
            if (tsmi is ToolStripMenuItem)
            {
                (tsmi as ToolStripMenuItem).Enabled = false;
            }
        }

        private void Tsmi_Click(object sender, EventArgs e)
        {
            if (sender is ToolStripMenuItem)
            {
                if ((sender as ToolStripMenuItem).Tag is Form)
                {
                    Form frm = (sender as ToolStripMenuItem).Tag as Form;
                    frm.WindowState = FormWindowState.Normal;
                }
            }
        }

        private void miCollection_Click(object sender, EventArgs e)
        {
            btnCUD frmCollection = new btnCUD();
            frmCollection.Show();
        }

        private void miDict_Click(object sender, EventArgs e)
        {
            FormDict frmDict = new FormDict();
            frmDict.Show();
        }
    }
}
